<?php

class m110630_043640_create_member extends CDbMigration {

	protected $_tables = array(
		'member'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'name'=>'string',
			'callSign'=>'string',
			'code'=>'string',
			'birthPlace'=>'string',
			'reference'=>'string',
			'gender'=>'string',
		),
		'memberAddress'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'street'=>'string',
			'city'=>'string',
			'province'=>'string',
			'postalCode'=>'string',
		),
		'memberDate'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'value'=>'integer',
		),
		'memberEquipment'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'serialNumber'=>'string',
			'misc'=>'text',
		),
		'memberIdentity'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'value'=>'string',
		),
		'memberIm'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'value'=>'string',
		),
		'memberImage'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'filename'=>'string',
			'extension'=>'string',
			'size'=>'integer',
			'type'=>'string',
		),
		'memberIndication'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'value'=>'string',
		),
		'memberOrganization'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'position'=>'string',
			'misc'=>'text',
		),
		'memberPhone'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'value'=>'string',
		),
		'memberUrl'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'ownerId'=>'integer',
			'memberId'=>'integer',
			'name'=>'string',
			'value'=>'string',
		),
		'refMemberCode'=>array(
			'year'=>'integer',
			'month'=>'integer',
			'value'=>'integer',
		)
	);

	protected $_indexes = array(
		'member'=>array(
			'updateTime',
			'ownerId',
			'name',
			'callSign',
			'code',
			'birthPlace',
			'reference',
			'gender',
		),
		'memberAddress'=>array(
			'ownerId',
			'memberId',
			'name',
			'city',
			'province',
			'postalCode',
		),
		'memberDate'=>array(
			'ownerId',
			'memberId',
			'name',
			'value',
		),
		'memberEquipment'=>array(
			'ownerId',
			'memberId',
			'name',
		),
		'memberIdentity'=>array(
			'ownerId',
			'memberId',
			'name',
			'value',
		),
		'memberIm'=>array(
			'ownerId',
			'memberId',
			'name',
			'value',
		),
		'memberImage'=>array(
			'ownerId',
			'memberId',
			'name',
		),
		'memberIndication'=>array(
			'ownerId',
			'memberId',
			'name',
			'value',
		),
		'memberOrganization'=>array(
			'ownerId',
			'memberId',
			'name',
			'position',
		),
		'memberPhone'=>array(
			'ownerId',
			'memberId',
			'name',
			'value',
		),
		'memberUrl'=>array(
			'ownerId',
			'memberId',
			'name',
			'value',
		),
	);

	public function safeUp() {
		foreach ($this->_tables as $key=>$value) {
			$this->createTable($key, $value);
		}
		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->createIndex($name, $table, $column);
			}
		}
	}

	public function safeDown() {
		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->dropIndex($name, $table, $column);
			}
		}
		foreach ($this->_tables as $key=>$value) {
			$this->dropTable($key);
		}
	}

}